package com.service;

import static com.def.GenericDef.E_XPPNLJDG_INVALID_INPUT;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPPNLJGE;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdJgeDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_prd_info;
import com.model.Ret_prd_jge;
import com.tx.Xppnljdg.XppnljgeI;
import com.tx.Xppnljdg.XppnljgeO;
import com.tx.Xppnljdg.XppnljgeOA;
import com.util.MqOpe;
import com.util.Util;

@Service("XppnljudgeService")
@Scope("prototype")
public class XppnljudgeService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XppnljudgeService.class);
	private static String TX_NAME = T_XPPNLJGE;

	private XppnljgeI inTrx;
	private XppnljgeO outTrx;
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetPrdJgeDao retPrdJgeDao;
	@Autowired
	private IBisDataDao bisDataDao;

	public String subMainProc(String strInMsg) {
		Session session = retPrdInfoDao.getCrSession();

		long rtnCode = _NORMAL;
		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		inTrx = new XppnljgeI();
		outTrx = new XppnljgeO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		mqOpe = new MqOpe();

		try {
			inTrx = (XppnljgeI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);

		}
		logger.info("InTrx is " + outMsg);
		return outMsg;

	}

	private long subMainProc2(char actionType) {
		long lRc = _NORMAL;
		switch (actionType) {
		// case 'J': {
		// lRc = judgeDefectFnc();
		// break;
		// }
		// case 'Q':
		// lRc = queryPrimaryDefectFnc();
		// break;
		case 'I':
			lRc = queryDetailDefectFnc();
			break;
		case 'B':
			lRc = updateAdminFlgByProc();
			break;
		default: {
			gszMsgBuf = "Invalide type input: [" + actionType + "]";
			return E_XPPNLJDG_INVALID_INPUT;
		}
		}

		return lRc;
	}

	public long queryDetailDefectFnc() {
		String prd_seq_id = inTrx.getPrd_seq_id();
		List<XppnljgeOA> oaryList = new ArrayList<XppnljgeOA>();
		String hql = "FROM Ret_prd_jge where id.prd_seq_id_fk ='" + prd_seq_id
				+ "' order by judge_timestamp, id.data_seq_id_fk ";

		List<Ret_prd_jge> retPrdJgeList = retPrdJgeDao.find(hql);
		for (Ret_prd_jge ret_prd_jge : retPrdJgeList) {
			if (ret_prd_jge.getDef_code1() != null) {
				XppnljgeOA oary = new XppnljgeOA();
				oary.setJudge_proc(ret_prd_jge.getProc_id());
				oary.setDefect_code(ret_prd_jge.getDef_code1());
				oary.setDefect_side(ret_prd_jge.getDef_code1_sd());
				oary.setPosition(ret_prd_jge.getDef_code1_lc());
				oary.setDefect_cnt(ret_prd_jge.getDef_code1_cnt());
				
				oaryList.add(oary);
			}else{
				continue;
			}
			if (ret_prd_jge.getDef_code2() != null) {
				XppnljgeOA oary = new XppnljgeOA();
				oary.setJudge_proc(ret_prd_jge.getProc_id());
				oary.setDefect_code(ret_prd_jge.getDef_code2());
				oary.setDefect_side(ret_prd_jge.getDef_code2_sd());
				oary.setPosition(ret_prd_jge.getDef_code2_lc());
				oary.setDefect_cnt(ret_prd_jge.getDef_code2_cnt());
				
				oaryList.add(oary);
			}else{
				continue;
			}
			if (ret_prd_jge.getDef_code3() != null) {
				XppnljgeOA oary = new XppnljgeOA();
				oary.setJudge_proc(ret_prd_jge.getProc_id());
				oary.setDefect_code(ret_prd_jge.getDef_code3());
				oary.setDefect_side(ret_prd_jge.getDef_code3_sd());
				oary.setPosition(ret_prd_jge.getDef_code3_lc());
				oary.setDefect_cnt(ret_prd_jge.getDef_code3_cnt());
				
				oaryList.add(oary);
			}else{
				continue;
			}
			if (ret_prd_jge.getDef_code4() != null) {
				XppnljgeOA oary = new XppnljgeOA();
				oary.setJudge_proc(ret_prd_jge.getProc_id());
				oary.setDefect_code(ret_prd_jge.getDef_code4());
				oary.setDefect_side(ret_prd_jge.getDef_code4_sd());
				oary.setPosition(ret_prd_jge.getDef_code4_lc());
				oary.setDefect_cnt(ret_prd_jge.getDef_code4_cnt());
				
				oaryList.add(oary);
			}else{
				continue;
			}
			if (ret_prd_jge.getDef_code5() != null) {
				XppnljgeOA oary = new XppnljgeOA();
				oary.setJudge_proc(ret_prd_jge.getProc_id());
				oary.setDefect_code(ret_prd_jge.getDef_code5());
				oary.setDefect_side(ret_prd_jge.getDef_code5_sd());
				oary.setPosition(ret_prd_jge.getDef_code5_lc());
				oary.setDefect_cnt(ret_prd_jge.getDef_code5_cnt());
				
				oaryList.add(oary);
			}else{
				continue;
			}
		}

		outTrx.setPrd_seq_id(prd_seq_id);
		outTrx.setOary(oaryList);
		if(oaryList.isEmpty()){
			outTrx.setDefect_cnt(0);
		}else{
			outTrx.setDefect_cnt(oaryList.size());
		}
		
		return _NORMAL;
	}
	
	private long updateAdminFlgByProc(){
		String prd_seq_id = inTrx.getPrd_seq_id();
		String proc_id = inTrx.getProc_id();
		String newQaFlg = inTrx.getNew_qa_flg();
		Ret_prd_info prd = new Ret_prd_info();
		
		prd = retPrdInfoDao.get(prd_seq_id);
		if (prd == null) {
			gszMsgBuf = "该产品不存在";
			return E_XPPNLJDG_INVALID_INPUT;
		}
		if ("JBQA".equals(proc_id)) {
			StringBuffer stringBuffer = new StringBuffer(prd.getPrd_admin_flg());
			if("Y".equals(newQaFlg)){
				   stringBuffer.setCharAt(4, 'Y');
				}else{
				   stringBuffer.setCharAt(4, 'N');
				}
			prd.setPrd_admin_flg(stringBuffer.toString());
		}else if ("DMQA".equals(proc_id)) {
			StringBuffer stringBuffer = new StringBuffer(prd.getPrd_admin_flg());
			if("Y".equals(newQaFlg)){
				   stringBuffer.setCharAt(5, 'Y');
				}else{
				   stringBuffer.setCharAt(5, 'N');
				}
			prd.setPrd_admin_flg(stringBuffer.toString());
		}
		retPrdInfoDao.update(prd);
		return _NORMAL;
	}
}
